<template>
  <div>
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @confirm="onConfirm"
    />
  </div>
</template>

<script>
// 导入日期格式化
import dayjs from 'dayjs'
import { editUser } from '@/api/user'
export default {
  name: 'updateBirthday',
  props: {
    userInfo: {
      required: true
    }
  },

  data () {
    return {
      currentDate: null,
      minDate: new Date(1984, 0, 1),
      maxDate: new Date(2035, 10, 1)
    }
  },

  mounted () {},

  methods: {
    async onConfirm (value) {
      try {
        const newData = dayjs(value).format('YYYY-MM-DD')
        await editUser({ birthday: newData })
        this.$toast.success('修改成功')
        this.$emit('closePop', false)
        // 修改父组件的名字
        // eslint-disable-next-line vue/no-mutating-props
        this.userInfo.birthday = newData
      } catch (error) {
        this.$toast.fail('修改失败')
      }
    }
  },

  created () {
    this.currentDate = new Date(this.userInfo.birthday)
  }
}
</script>

<style lang="less" scoped>
</style>
